<template>
  <div style="width: 80%">
    <div style="margin-bottom: 30px">新增单车</div>
    <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm" label-width="100px">
      <!-- pro与rules中的属性对应 -->
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入单车名称信息"></el-input>
      </el-form-item>
      <el-form-item label="出厂日期" prop="date">
        <el-date-picker v-model="form.date" placeholder="请选择单车出厂日期" type="date" value-format="yyyy-MM-dd"></el-date-picker>
      </el-form-item>
      <el-form-item label="分类" prop="categories">
        <el-cascader v-model="form.categories" :options="categories" :props="{value: 'name', label: 'name'}">

        </el-cascader>
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:9090/api/bike/file/upload"
            :on-preview="handlePreview"
            :show-file-list="false"
            :on-success="handleCoverSuccess">
          <img v-if="form.cover" :src="form.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="数量" prop="nums">
        <el-input v-model="form.nums" placeholder="请输入单车数量"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="form.description" placeholder="请输入单车名称信息" type="textarea" style="width: 500px"></el-input>
      </el-form-item>
    </el-form>

    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="save" size="medium">提交</el-button>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";

export default {
  name: 'AddBike',
  data() {
    const checkNums = (rule, value, callback) => {
      value = parseInt(value);
      if(!Number.isInteger(value) || value <= 0) {
        callback(new Error('请输入正整数'));
      }
      callback();
    }
    return {
      admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {},
      form: { cover:'' },
      categories: [],
      rules: {
        name: [
          {required: true, message: '请输入单车名称', trigger: 'blur'},
        ],
        nums: [
          {validator: checkNums, trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    request.get('/category/tree').then(res => {
      this.categories = res.data;
    })
  },
  methods: {
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request.post('/bike/', this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success('新增成功');
              this.$refs['ruleForm'].resetFields();
            } else {
              this.$notify.error(res.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    handlePreview(){
      this.previewVisible = true;
      this.previewPath = file.response.data.url;
    },
    handleCoverSuccess(res){
      if (res.code === '200') {
        this.form.cover = res.data;
      }
    }
  }

}

</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

